package com.snda.sc.auth;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.snda.sc.auth.data.GameHost;
import com.snda.sc.utils.Constants;

public class RedirectResult extends AbstractAuthResult {

	private GameHost gameHost;
	private String failureUrl;

	public RedirectResult(GameHost gameHost, String failureUrl) {
		this.gameHost = gameHost;
		this.failureUrl = failureUrl;
	}

	@Override
	public void doRender(HttpServletRequest request, HttpServletResponse response) throws IOException {
		if (result != Constants.ResultCode.AUTH_OK) {
			response.sendRedirect(failureUrl);
			return;
		}
		String json = JSON.toJSONString(mergeProp());
		String url = gameHost.getUrl() + "?s=" + gameHost.getAppArea() + (isTestEnv() ? "&testEnv=true" : "") + "&json="
				+ URLEncoder.encode(json, "UTF-8");
		response.sendRedirect(url);
	}
}
